package com.iotteam.androidbase.moudules.login.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.ToolbarActivity;
import com.iotteam.androidbase.commen.utils.ResUtil;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.commen.views.TimerButton;
import com.iotteam.androidbase.moudules.login.presenter.IRegistPresenter;
import com.iotteam.androidbase.moudules.login.presenter.RegistPresenterCompl;

/**
 * Created by lidechen on 5/7/17.
 */

public class RegistActivity extends ToolbarActivity implements IRegistView, View.OnClickListener{

    private TimerButton mSendButton;

    private EditText mMobleEdit;
    private EditText mPasswordEdit;
    private EditText mSmsCodeEdit;
    private EditText mRepasswordEdit;
    private Button mRegistBtn;

    private IRegistPresenter mRegistPresenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_regist;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRegistPresenter = new RegistPresenterCompl(RegistActivity.this);

        mSendButton = (TimerButton) findViewById(R.id.tb_send);
        mSendButton.setMainText(RegistActivity.this.getResources().getString(R.string.hint_sendcheckcode));
        mSendButton.setTimeoutText(RegistActivity.this.getResources().getString(R.string.hint_resendcheckcode));

        mMobleEdit = (EditText) findViewById(R.id.edit_text_mobile);
        mPasswordEdit = (EditText) findViewById(R.id.edit_text_password);
        mRepasswordEdit = (EditText) findViewById(R.id.edit_text_repassword);
        mSmsCodeEdit = (EditText) findViewById(R.id.edit_text_smscode);
        mRegistBtn = (Button) findViewById(R.id.btn_regist);

        mRegistBtn.setOnClickListener(this);

        mSendButton.setOnTimerListener(new TimerButton.OnTimerListener() {
            @Override
            public boolean onStart() {
                mWaitDialog.setContent(ResUtil.getStringById(R.string.dialog_smsing));
                return mRegistPresenter.doSmsCode();
            }

            @Override
            public void onStop() {
                mRegistPresenter.canceldoSmsCode();
            }
        });
    }

    @Override
    public String getMobile() {
        return mMobleEdit.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return mPasswordEdit.getText().toString().trim();
    }

    @Override
    public String getRepassword() {
        return mRepasswordEdit.getText().toString().trim();
    }

    @Override
    public String getSmscode() {
        return mSmsCodeEdit.getText().toString().trim();
    }

    @Override
    public void showLoading() {
        mWaitDialog.show();
    }

    @Override
    public void dismissLoading() {
        mWaitDialog.dismiss();
    }

    @Override
    public void showError(String err) {
        ToastUtil.showLong(err);
    }

    public static void launch(Context context) {
        context.startActivity(new Intent(context, RegistActivity.class));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.btn_regist){
            mWaitDialog.setContent(ResUtil.getStringById(R.string.dialog_signuping));
            mRegistPresenter.doRegister();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRegistPresenter.canceldoSmsCode();
        mRegistPresenter.canceldoRegister();
    }
}
